package com.invoicing.controller;

import com.invoicing.MainController;
import com.invoicing.Service.CommodityService;
import com.invoicing.Service.TypeService;
import com.invoicing.entity.Commodity;
import com.invoicing.entity.CommodityData;
import com.invoicing.view.MenuView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import org.springframework.beans.factory.annotation.Autowired;
import javafx.scene.control.cell.PropertyValueFactory;
import sun.reflect.generics.tree.TypeSignature;


import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

@FXMLController
public class StockController implements Initializable {

    private List<Commodity> commodityList = new ArrayList<Commodity>();

    private ObservableList<CommodityData> data = FXCollections.observableArrayList();

    private ObservableList<CommodityData> dataWarn = FXCollections.observableArrayList();

    @Autowired
    CommodityService commodityService;
    @Autowired
    private TypeService typeService;
    @FXML
    private TableView table;
    @FXML
    private TableView warnTable;
    @FXML
    private TableColumn id;
    @FXML
    private TableColumn name;
    @FXML
    private TableColumn type;
    @FXML
    private TableColumn stock;
    @FXML
    private TableColumn warn;
    @FXML
    private TableColumn warn_id;
    @FXML
    private TableColumn warn_name;
    @FXML
    private TableColumn warn_type;
    @FXML
    private TableColumn warn_stock;
    @FXML
    private TextField input_id;






    public void initialize(URL location, ResourceBundle resources) {

        id.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("id"));
        name.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("name"));
        type.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("type"));
        stock.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("stock"));
        warn.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("alert"));
        warn_id.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("id"));
        warn_name.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("name"));
        warn_type.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("type"));
        warn_stock.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("stock"));

        commodityList = commodityService.listAll();
        String string = "aa";
        for(int i=0;i<commodityList.size();i++){
            try{
                string = typeService.findById(commodityList.get(i).getTypeId()).getTypeName();
            }catch (Exception e){

            }
            CommodityData commodityData = new CommodityData(commodityList.get(i),0,string,0,0,0,0);
            if(commodityList.get(i).getStock()<commodityList.get(i).getAlert()){//库存紧张
                dataWarn.add(commodityData);
            }
            data.add(commodityData);
        }
        table.setItems(data);
        warnTable.setItems(dataWarn);

    }

    @FXML
    private void showOne(){
        int id = 0;
        if(input_id.getText().length()!=0){
            id = Integer.parseInt(input_id.getText());
            for(int j=0;j<data.size();j++){
                if(data.get(j).getId()==id){
                    CommodityData commodityDataTemp = data.get(j);
                    data.clear();
                    data.add(commodityDataTemp);
                    table.refresh();
                }
            }
        }
    }

    @FXML
    private void showAll(){
        data.clear();
        input_id.clear();
        String string = "aa";
        commodityList = commodityService.listAll();
        for(int i=0;i<commodityList.size();i++){
            try{
                string = typeService.findById(commodityList.get(i).getTypeId()).getTypeName();
            }catch (Exception e){
            }
            CommodityData commodityData = new CommodityData(commodityList.get(i),0,string,0,0,0,0);
            data.add(commodityData);

        }
        table.refresh();
    }
    @FXML
    private void setFlesh(){
        showAll();
        dataWarn.clear();
        commodityList = commodityService.listAll();
        String string = "aa";
        for(int i=0;i<commodityList.size();i++){
            try{
                string = typeService.findById(commodityList.get(i).getTypeId()).getTypeName();
            }catch (Exception e){
            }
            CommodityData commodityData = new CommodityData(commodityList.get(i),0,string,0,0,0,0);
            if(commodityList.get(i).getStock()<commodityList.get(i).getAlert()){//库存紧张
                dataWarn.add(commodityData);
            }

        }
        warnTable.setItems(dataWarn);
    }

    @FXML
    private void setBack(){  MainController.showView(MenuView.class);}
}
